package com.suoluo.zenapp.controllers.admin.dest;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

import com.suoluo.zenapp.common.APPConstants;
import com.suoluo.zenapp.common.CommonResp;
import com.suoluo.zenapp.common.JSONPageData;
import com.suoluo.zenapp.common.PageUtil;
import com.suoluo.zenapp.common.encrypt.EncryptUtil;
import com.suoluo.zenapp.controllers.JSONInterceptor.JSON;
import com.suoluo.zenapp.pojo.dest.DestExtPOJO;
import com.suoluo.zenapp.pojo.dest.DestImgResourcePOJO;
import com.suoluo.zenapp.pojo.dest.DestResourceExtPOJO;
import com.suoluo.zenapp.pojo.dest.DestResourcePOJO;
import com.suoluo.zenapp.pojo.dest.DestTrafficPOJO;
import com.suoluo.zenapp.pojo.dest.DestVersionPOJO;
import com.suoluo.zenapp.service.dest.DestService;
import com.suoluo.zenapp.vo.DestImgResourceVO;
import com.suoluo.zenapp.vo.DestResourceVO;
import com.suoluo.zenapp.vo.ParameterVO;

@Path("/")
public class DestController {

	@Resource(name = "destService")
	private DestService destService;

	/*********************************** 目的地管理代码 ***start ***************************/
	/**
	 * 跳转到目的地和目的地资源列表页面
	 * 
	 * @return
	 */
	@Get("list")
	public String list() {
		return "listDest";
	}

	/**
	 * 获取目的地和目的地资源列表页面数据
	 * 
	 * @return
	 */
	@JSON
	@Post("listDest")
	public Object listDest(ParameterVO para) {
		int start = PageUtil.calcStartRow(para.getPage(), para.getRows());
		para.setStartPage(start);
		JSONPageData data = new JSONPageData();

		data.setTotal(destService.findDestResourceCount());

		data.setRows(destService.findListDest(para));

		return data;
	}

	/**
	 * 跳转到添加目的地页面
	 * 
	 * @return
	 */
	@Get("addDest")
	public String addDest() {
		return "addDest";
	}

	/**
	 * 保存目的地信息
	 * 
	 * @param dest
	 * @return
	 */
	@JSON
	@Post("saveDest")
	public Object saveDest(DestResourceVO dest) throws Exception {
		dest.setNodeCount(APPConstants.ZERO);
		dest.setParentId(APPConstants.ZERO);
		dest.setResourceType(APPConstants.RESOURCE_DEST);
		dest.setLayerIndex(APPConstants.ONE);
		dest.setTreeIndex(APPConstants.ONE);
		CommonResp resp = destService.saveDest(dest);
		
		return resp;

	}

	/**
	 * 跳转到修改目的地页面
	 * 
	 * @return
	 */
	@Get("toUpdateDest")
	public String toUpdateDest(Invocation inv,@Param("destId")int destId) {
		DestResourceVO dest = destService.findDestById(destId);
		inv.addModel("dest", dest);
		DestExtPOJO destExt = destService.findDestExtById(destId);
		inv.addModel("destExt",destExt);
		return "updateDest";
	}

	/**
	 * 修改目的地信息
	 * 
	 * @param dest
	 * @return
	 * @throws Exception
	 */
	@JSON
	@Post("updateDest")
	public Object updateDest(DestResourceVO dest) throws Exception {
		destService.updateDest(dest);
		return CommonResp.SUCCESS;
	}

	/*********************************** 目的地管理代码 ***end ***************************/
	/*********************************** 目的地资源类别管理代码****start **************************/

	/**
	 * 跳转到资源项页面
	 * 
	 * @param id
	 * @return
	 */
	@Get("toListType")
	public String toListType(Invocation invo) {
		int destId = Integer.parseInt(invo.getParameter("destId"));
		invo.addModel("destId", destId);
		String destName = destService.findName(destId);
		invo.addModel("destName", destName);
		return "listType";
	}

	/**
	 * 查询资源项信息
	 * 
	 * @param destId
	 * @return
	 */
	@JSON
	@Post("listType")
	public Object listType(ParameterVO param) {
		int destId = param.getDestId();
		return destService.findListResourceType(destId);
	}

	@Get("addType")
	public String addType(Invocation inv,@Param("destId")int destId) {
		DestResourceVO dest = destService.findDestById(destId);
		inv.addModel("relId",dest.getRelId());
		return "addType";
	}

	@JSON
	@Post("saveType")
	public Object saveType(DestResourceVO dest) {
		this.destService.saveType(dest);
		return CommonResp.SUCCESS;
	}

	@Get("toUpdateType")
	public String toUpdateType(Invocation invo,@Param("destId")int destId) {
		int resourceTypeId = Integer.parseInt(invo.getParameter("id"));
		DestResourcePOJO resourceType = destService
				.findDestById(resourceTypeId);
		invo.addModel("resourceType", resourceType);
		
		DestResourceVO dest = destService.findDestById(destId);
		invo.addModel("relId",dest.getRelId());
		return "updateType";
	}

	/**
	 * 修改目的地类别信息
	 * 
	 * @param dest
	 * @return
	 * @throws Exception
	 */
	@JSON
	@Post("updateType")
	public Object updateType(DestResourceVO dest) throws Exception {
		destService.updateDestType(dest);
		return CommonResp.SUCCESS;
	}

	/*********************************** 目的地资源类别管理代码****end **************************/
	/*********************************** 资源项管理代码****start **************************/

	/**
	 * 跳转到资源项页面
	 * 
	 * @param id
	 * @return
	 */
	@Get("toListItem")
	public String toListItem(Invocation invo) {
		int resourceTypeId = Integer.parseInt(invo.getParameter("id"));
		int destId = Integer.parseInt(invo.getParameter("destId"));
		String destName = destService.findName(destId);
		invo.addModel("destName", destName);
		String resourceName = destService.findName(resourceTypeId);
		invo.addModel("resourceName",resourceName);
		invo.addModel("resourceTypeId", resourceTypeId);
		invo.addModel("destId", destId);
		return "listItem";
	}

	/**
	 * 查询资源项信息
	 * 
	 * @param destId
	 * @return
	 */
	@JSON
	@Post("listItem")
	public Object listItem(@Param("id") int typeId) {
		return destService.findListResourceTypeItem(typeId);
	}

	@Get("addItem")
	public String addItem(Invocation inv,@Param("resourceTypeId")int resourceTypeId) {
		DestResourceVO feature = destService.findDestById(resourceTypeId);
		inv.addModel("featureId",feature.getRelId());
		return "addItem";
	}

	@JSON
	@Post("saveItem")
	public Object saveItem(DestResourceVO destVO) throws Exception{
		// 1.根据添加的类别信息去数据库查询看是否已经添加了同类型，如已添加，则不能重复添加
		//int destId = dest.getDestId();
		int parentId = destVO.getParentId();
		destVO.setNodeCount(APPConstants.ZERO);
		destVO.setResourceType(APPConstants.RESOURCE_ITEM);
		// 获取同一父节点下最大的顺序
		Integer maxOrder = destService.findMaxOrderByParentId(parentId);
		if (maxOrder != null) {
			int temp = maxOrder + 1;
			destVO.setLayerIndex(temp);
		} else {
			destVO.setLayerIndex(APPConstants.ONE);
		}
		destVO.setTreeIndex(APPConstants.ZERO);
		
		return destService.saveItem(destVO);
		
	}

	@Get("toUpdateItem")
	public String toUpdateItem(Invocation invo,@Param("resourceTypeId")int resourceTypeId) {
		int resourceTypeItemId = Integer.parseInt(invo.getParameter("id"));
		DestResourcePOJO resourceTypeItem = destService
				.findDestById(resourceTypeItemId);
		invo.addModel("resourceTypeItem", resourceTypeItem);
		DestResourceExtPOJO resourceExt = destService.findDestResourceExtPOJOById(resourceTypeItemId);
		invo.addModel("resourceExt",resourceExt);
		
		DestResourceVO feature = destService.findDestById(resourceTypeId);
		invo.addModel("featureId",feature.getRelId());
		return "updateItem";
	}

	/**
	 * 修改目的地信息
	 * 
	 * @param dest
	 * @return
	 * @throws Exception
	 */
	@JSON
	@Post("updateItem")
	public Object updateItem(DestResourceVO destVO) throws Exception {
		destService.updateItem(destVO);
		return CommonResp.SUCCESS;
	}

	/*********************************** 资源项管理代码****end **************************/
	/*********************************** 应用版本管理代码 ******************************/

	/**
	 * 跳转到应用版本管理页面
	 * 
	 * @return
	 */
	@Get("toListVersion")
	public String toListVersion() {
		return "listVersion";
	}

	/**
	 * 查询所有应用版本信息
	 * 
	 * @return
	 */
	@JSON
	@Post("listVersion")
	public Object listVersion(ParameterVO param) {
		int startPage = PageUtil.calcStartRow(param.getPage(), param.getRows());
		param.setStartPage(startPage);
		List<DestVersionPOJO> list = destService.findDestVersion(param);
		JSONPageData data = new JSONPageData();
		data.setRows(list);
		int total = destService.findVersionTotal();
		data.setTotal(total);
		return list;
	}

	@Get("addVersion")
	public String addVersion() {
		return "addVersion";
	}

	@JSON
	@Post("saveVersion")
	public Object saveVersion(DestVersionPOJO version) {
		DestVersionPOJO pojo = destService.findVersionByDestId(version
				.getDestId());
		if (pojo != null) {
			CommonResp resp = CommonResp.FAIL;
			resp.setResultMsg("同一目的地应用的版本信息已存在，请修改版本号即可！");
			return resp;
		} else {
			// 调用md5接口生成一个16位的随机字符串，设置为该应用的token
			String token = EncryptUtil.randomString(16);
			version.setToken(token);
			destService.saveVersion(version);
			return CommonResp.SUCCESS;
		}
	}

	/**
	 * 跳转到修应用改版本信息页面
	 * 
	 * @param invo
	 * @param versionId
	 * @return
	 */
	public String toUpdateVersion(Invocation invo,
			@Param("versionId") int versionId) {
		DestVersionPOJO pojo = destService.findDestVersionById(versionId);
		invo.addModel("version", pojo);
		return "updateVersion";
	}

	/**
	 * 更新版本信息
	 * 
	 * @return
	 */
	@JSON
	@Post("updateVersion")
	public Object updateVersion(DestVersionPOJO version) {
		destService.updateVersion(version);
		return CommonResp.SUCCESS;
	}
	
	/**
	 * 添加版本时查询所有目的地信息
	 * @return
	 */
	@JSON
	@Post("listDestName")
	public List<DestResourcePOJO> listDestName()
	{
		return destService.findListDestName();
	}
/*******************************version*****end*********************************************/
/*******************************imeages****start****************************************************/
	/**
	 * 跳转到目的地多图界面
	 */
	@Get("toListDestImgs")
	public String toListDestImgs(Invocation invo){
		int destId = Integer.parseInt(invo.getParameter("destId"));
		invo.addModel("destId",destId);
		
		String destName = destService.findName(destId);
		invo.addModel("destName", destName);
		return "listDestImgs";
	}
	/**
	 * 查询目的地下的多图信息
	 * @return
	 */
	@JSON
	@Post("listDestImgs")
	public Object listDestImgs(@Param("destId")int destId)
	{
		return destService.findListImgs(destId);
	}
	
	@Get("addDestImgs")
	public String addDestImgs(Invocation invo)
	{
		return "addImgs";
	}
	@JSON
	@Post("saveDestImgs")
	public Object saveDestImgs(DestResourceVO imgVO)
	{
		return destService.saveDestImgs(imgVO);
	}

	
	@SuppressWarnings("rawtypes")
	@Get("refer")
	public String showRefer(Invocation inv,@Param("type")int typeId){
		List<Map> list = null;
		//添加目的地类型
		if(typeId == 1)
		{
			String name = inv.getParameter("destName");
			list = destService.findZentravelDestByName(name);
		}
		else if(typeId == 2)//添加目的地特色类型
		{
			int destId = Integer.parseInt(inv.getParameter("destId"));
			list = destService.findZentravelDestFeature(destId);
		}
		else if(typeId == 3)//添加目的地的特色项
		{
			int featureId = Integer.parseInt(inv.getParameter("featureId"));
			String itemName = inv.getParameter("itemName");
			list  = destService.findZentravelDestFeatureItem(featureId, itemName);
		}
	
		inv.addModel("list", list);
		return "reference";
	}


/*******************************imeages****end****************************************************/
/*******************************Item imeages****start****************************************************/
	/**
	 * 跳转到目的地资源项多图界面
	 */
	@Get("toListItemImgs")
	public String toListItemImgs(Invocation invo){
		int itemId = Integer.parseInt(invo.getParameter("itemId"));
		int destId = Integer.parseInt(invo.getParameter("destId"));
		int resourceTypeId = Integer.parseInt(invo.getParameter("resourceTypeId"));
		String destName = destService.findName(destId);
		invo.addModel("destName", destName);
		String resourceName = destService.findName(resourceTypeId);
		invo.addModel("resourceName",resourceName);
		String itemName = destService.findName(itemId);
		invo.addModel("resourceTypeId",resourceTypeId);
		invo.addModel("itemName",itemName);
		invo.addModel("itemId",itemId);
		invo.addModel("destId",destId);
		return "listItemImgs";
	}
	/**
	 * 查询特色项的多图信息
	 * @return
	 */
	@JSON
	@Post("listItemImgs")
	public Object listItemImgs(@Param("itemId")int itemId)
	{
		return destService.findListImgs(itemId);
	}
	
	@Get("addItemImgs")
	public String addItemImgs(Invocation invo)
	{
		return "addImgs";
	}
	@JSON
	@Post("saveItemImgs")
	public Object saveItemImgs(DestResourceVO imgVO)
	{
		return destService.saveItemImgs(imgVO);
	}
	
	@Get("toUpdateItemImg")
	public String toUpdateItemImg(Invocation inv)
	{
		int imgId = Integer.parseInt(inv.getParameter("imgId"));
		DestImgResourcePOJO imgPOJO = destService.findDestResourceImgById(imgId);
		inv.addModel("img",imgPOJO);
		return "updateImgs";
	}
	@JSON
	@Post("updateItemImg")
	public Object updateItemImg(DestImgResourceVO imgVO)
	{
		 destService.updateDestResourceImg(imgVO);
		 return CommonResp.SUCCESS;
	}
	/*******************************Item imeages****end**************************************/
  
	/*******************************destTraffic****start****************************************/
	@Get("traffic/vlist")
	public String toListDestTraffic(Invocation invo,@Param("destId")int destId)
	{
		invo.addModel("destId",destId);
		return "listDestTraffic";
	}
	@JSON
	@Post("traffic/list")
	public Object listDestTraffic(@Param("destId")int destId)
	{
		return destService.findListDestTraffic(destId);
	}
	@Get("traffic/vsave")
	public String toAddDestTraffic()
	{
		return "addDestTraffic";
	}
	@JSON
	@Post("traffic/save")
	public Object saveDestTraffic(DestTrafficPOJO traffic)
	{
		destService.saveDestTraffic(traffic);
		return CommonResp.SUCCESS;
	}
	
	@Get("traffic/vupdate")
	public String toUpdateDestTraffic(Invocation invo,@Param("id")int trafficId)
	{
		DestTrafficPOJO traffic = destService.findDestTrafficById(trafficId);
		invo.addModel("destTraffic",traffic);
		return "updateDestTraffic";
	}
	@JSON
	@Post("traffic/update")
	public Object updateDestTraffic(DestTrafficPOJO traffic)
	{
		destService.updateDestTraffic(traffic);
		return CommonResp.SUCCESS;
	}
	@JSON
	@Post("traffic/delete")
	public Object deleteDestTraffic(@Param("id")int trafficId)
	{
		destService.deleteDestTraffic(trafficId);
		return CommonResp.SUCCESS;
	}
	/*******************************destTraffic****end****************************************/
	/**
	 * 给目的地资源排序
	 */
	@Get("destOrder")
	public String destOrder(@Param("destId")int destId)
	{
		
		destService.updateDestResourcesSort(destId);
		return list();
	}
	/**
	 * 删除图片信息
	 * @param invo
	 * @return
	 */
	@JSON
	@Post("deleteDestImg")
	public Object deleteDestImg(DestImgResourcePOJO img){
		destService.deleteDestImg(img);
		return CommonResp.SUCCESS;
	}
	/**
	 * 删除特色项图片信息
	 * @param invo
	 * @return
	 */
	@JSON
	@Post("deleteItemImg")
	public Object deleteItemImg(DestImgResourcePOJO img){
		destService.deleteItemImg(img);
		return CommonResp.SUCCESS;
	}
//	@SuppressWarnings("rawtypes")
//	@JSON
//	@Post("zendest")
//	public Object zentravelDest(@Param("destName")String destName)
//	{
//		List<Map> list = destService.findZentravelDestFeatureItem();
//		CommonResp comm = CommonResp.SUCCESS;
//		comm.setListMap(list);
//		return comm;
//	}
}
